import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from "element-plus"

import HomePage from "@/views/Home/HomePage.vue"
import LoginPage from "@/views/Login/LoginPage.vue"

import System from "@/components/System/System.vue"
import BannerList from "@/components/Banner/BannerList.vue"
import CreateBanner from "@/components/Banner/CreateBanner.vue"
import ProductList from "@/components/Product/ProductList.vue"
import FilterList from "@/components/Product/FilterList.vue"
import UserList from "@/components/AccountManagement/UserList.vue"
import AdminList from "@/components/AccountManagement/AdminList.vue"
import ShoppingCar from "@/components/ShoppingCar/ShoppingCar.vue"
import ECharts from "@/components/DataVisualization/ECharts.vue"
import Highcharts from "@/components/DataVisualization/Highcharts.vue"
import Antv from "@/components/DataVisualization/Antv.vue"
import RichText from "@/components/Editor/RichText.vue"
import Markdown from "@/components/Editor/Markdown.vue"
import ImportPage from "@/components/Import&Export/ImportPage.vue"
import ExportPage from "@/components/Import&Export/ExportPage.vue"
import MapPage from "@/components/Map/MapPage.vue"

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: LoginPage },
  {
    path: "/home",
    component: HomePage,
    children: [
      {
        path: "/system",
        component: System,
        title: "系统首页"
      },
      {
        path: "/banner/list", component: BannerList,
        title: "轮播图管理",
        parent: { title: "轮播图列表", to: "/banner/list" }
      },
      {
        path: "/banner/create", component: CreateBanner,
        title: "轮播图管理",
        parent: { title: "创建轮播图", to: "/banner/create" }
      },
      {
        path: "/product/list", component: ProductList,
        title: "产品管理",
        parent: { title: "产品列表", to: "/product/list" }
      },
      {
        path: "/filter/list", component: FilterList,
        title: "产品管理",
        parent: { title: "刷选列表", to: "/filter/list" }
      },
      {
        path: "/user/list", component: UserList,
        title: "账户管理",
        parent: { title: "用户列表", to: "/user/list" }
      },
      {
        path: "/admin/list", component: AdminList,
        title: "账户管理",
        parent: { title: "管理员列表", to: "/admin/list" }
      },
      {
        path: "/shoppingcar", component: ShoppingCar,
        title: "购物车管理"
      },
      {
        path: "/data/echarts", component: ECharts,
        title: "数据可视化",
        parent: { title: "ECharts", to: "/data/echarts" }
      },
      {
        path: "/data/highcharts", component: Highcharts,
        title: "数据可视化",
        parent: { title: "Highcharts", to: "/data/highcharts" }
      },
      {
        path: "/data/antv",
        component: Antv,
        title: "数据可视化",
        parent: { title: "Antv", to: "/data/antv" }
      },
      {
        path: "/editor/richtext", component: RichText,
        title: "编辑器",
        parent: { title: "富文本编辑器", to: "/editor/richtext" }
      },
      {
        path: "/editor/markdown", component: Markdown,
        title: "编辑器",
        parent: { title: "Markdown编辑器", to: "/editor/markdown" }
      },
      {
        path: "/import", component: ImportPage,
        title: "导入以及导出",
        parent: { title: "导入", to: "/import" }
      },
      {
        path: "/export", component: ExportPage,
        title: "导入以及导出",
        parent: { title: "导出", to: "/export" }
      },
      {
        path: "/map",
        component: MapPage,
        title: "百度地图"
      },
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});


router.beforeEach((to) => {
  const userLS = JSON.parse(localStorage.getItem("userInfo"));
  if (to.path != "/login" && !userLS?.token) {
    ElMessage.error("请先登录再进来");
    return { path: "/" };
  }
});

const filterRoutes = (routes) => {
  const items = routes[2]?.children || [];
  // 定义输出数据
  const menus = [];
  items.forEach((item) => {
    if (menus.length === 0) {
      // menus里没有数据
      menus.push({
        title: item.title,
        to: item.path,
        children: [],
      });
      if (item.parent)
        menus[0].children.push({
          title: item.parent.title,
          to: item.parent.to,
        });
    } else {
      // 如果menus里已经有数据了
      const tmp = menus.find((v) => {
        return v.title === item.title;
      });
      if (tmp) {
        tmp.children.push({
          title: item.parent.title,
          to: item.parent.to,
        });
      } else {
        menus.push({
          title: item.title,
          to: item.path,
          children: [],
        });
        if (item.parent)
          menus[menus.length - 1].children.push({
            title: item.parent.title,
            to: item.parent.to,
          });
      }
    }
  });
  return menus;
};

export const menus = filterRoutes(routes);

export default router;
